package com.tripwego.client.ui.widget;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasMouseOutHandlers;
import com.google.gwt.event.dom.client.HasMouseOverHandlers;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.datepicker.client.CalendarUtil;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.tripwego.client.event.AddTrajetEvent;
import com.tripwego.client.event.AddTrajetEventHandler;
import com.tripwego.client.event.UpdateEtapeEvent;
import com.tripwego.client.event.UpdateEtapeEventHandler;
import com.tripwego.client.model.EtapeModel;
import com.tripwego.client.model.TrajetModel;
import com.tripwego.client.ui.pageVoyage.FormEditEtapeWidget;
import com.tripwego.client.ui.pageVoyage.IVoyageView;

public class EtapeHeaderWidget extends Composite implements IVoyageView, ClickHandler, UpdateEtapeEventHandler, HasMouseOverHandlers, HasMouseOutHandlers,
		AddTrajetEventHandler {

	private Presenter presenter;

	private EventBus eventBus;

	private EtapeModel etapeModel = null;

	private EtapeWidget etapeWidget = null;

	/*
	 * Elements graphiques
	 */
	private FlexTable viewPanel = new FlexTable();

	private HorizontalPanel panel_actions = new HorizontalPanel();

	private Image image_icon = new Image();
	private Label lbl_name = new Label();
	private Label lbl_date = new Label();

	private FormEditEtapeWidget formEditEtapeWidget = new FormEditEtapeWidget();
	private DialogBox dialogBox_editEtape = null;

	private Image image_detail = new Image("tripwego/sc/skins/Enterprise/images/headerIcons/maximize.png");

	private Image image_update = new Image("tripwego/sc/skins/Enterprise/images/headerIcons/settings.png");

	private Image image_delete = new Image("tripwego/sc/skins/Enterprise/images/headerIcons/close.png");

	/**
	 * 
	 */
	public EtapeHeaderWidget(EtapeWidget etapeWidget) {
		super();

		this.etapeWidget = etapeWidget;

		viewPanel.setStyleName("EtapeHeaderWidget");
		viewPanel.setSize("350px", "");

		dialogBox_editEtape = (DialogBox) formEditEtapeWidget.asWidget();

		panel_actions.setStyleName("horizontalPanel_actions");

		viewPanel.setWidget(0, 0, image_icon);
		viewPanel.getCellFormatter().setHeight(0, 0, "");
		image_icon.setSize("24px", "24px");
		viewPanel.getCellFormatter().setWidth(0, 0, "1%");

		viewPanel.setWidget(0, 1, lbl_name);
		viewPanel.getCellFormatter().setWidth(0, 1, "200px");
		viewPanel.getCellFormatter().setHeight(0, 1, "1%");
		lbl_name.setWidth("");
		lbl_name.setStyleName("lblTitleEtape");

		initWidget(viewPanel);
		lbl_date.setText("99 jours");
		lbl_date.setWordWrap(false);
		lbl_date.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		lbl_date.setStyleName("lbl_nbDay");
		viewPanel.setWidget(0, 2, lbl_date);
		viewPanel.getCellFormatter().setWidth(0, 2, "");
		lbl_date.setWidth("");

		panel_actions.setSpacing(2);
		panel_actions.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		viewPanel.setWidget(0, 3, panel_actions);
		viewPanel.getCellFormatter().setHeight(0, 3, "1%");
		panel_actions.setSize("", "");
		viewPanel.getCellFormatter().setWidth(0, 3, "1%");

		image_icon.setTitle("Centrer sur l'\u00E9tape");
		image_detail.setTitle("D\u00E9tail de l'\u00E9tape");
		panel_actions.add(image_detail);
		image_update.setTitle("Modifier l'\u00E9tape");
		panel_actions.add(image_update);
		image_delete.setTitle("Supprimer l'\u00E9tape");
		panel_actions.add(image_delete);

		image_detail.setStyleName("img_action");
		image_update.setStyleName("img_action");
		image_delete.setStyleName("img_action");

		viewPanel.getCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_RIGHT);

		viewPanel.getFlexCellFormatter().setRowSpan(0, 0, 1);
		viewPanel.getFlexCellFormatter().setRowSpan(0, 3, 1);
		viewPanel.getCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_MIDDLE);
		viewPanel.getCellFormatter().setVerticalAlignment(0, 2, HasVerticalAlignment.ALIGN_MIDDLE);
		viewPanel.getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_MIDDLE);
		viewPanel.getCellFormatter().setVerticalAlignment(0, 3, HasVerticalAlignment.ALIGN_TOP);
		viewPanel.getCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_LEFT);
		FlexTableHelper.fixRowSpan(viewPanel);

		viewPanel.addClickHandler(this);
		image_icon.addClickHandler(this);
		image_detail.addClickHandler(this);
		image_update.addClickHandler(this);
		image_delete.addClickHandler(this);

		addMouseOverHandler(new MouseOverHandler() {

			@Override
			public void onMouseOver(MouseOverEvent event) {
				majPanelActions(false);
			}
		});

		addMouseOutHandler(new MouseOutHandler() {

			@Override
			public void onMouseOut(MouseOutEvent event) {
				majPanelActions(true);
			}
		});
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
		this.formEditEtapeWidget.setPresenter(presenter);
	}

	@Override
	public void setEventBus(EventBus eventBus) {
		this.eventBus = eventBus;
		this.formEditEtapeWidget.setEventBus(eventBus);

		// ecouteurs d'evenements
		eventBus.addHandler(UpdateEtapeEvent.TYPE, this);
		eventBus.addHandler(AddTrajetEvent.TYPE, this);
	}

	@Override
	public void init() {
		if (etapeModel != null) {
			image_icon.setUrl("icons/number_" + etapeModel.getOrder() + ".png");
			lbl_name.setText(etapeModel.getName());

			Integer nbDay = 1;

			if (!etapeModel.isFirstStep()) {
				nbDay = CalendarUtil.getDaysBetween(etapeModel.getDateDeb(), etapeModel.getDateFin());
			}

			if (nbDay == 1) {
				lbl_date.setText("1 jour");
			} else {
				lbl_date.setText(nbDay.toString() + " jours");
			}
		}

		majPanelActions(true);
	}

	@Override
	public void onClick(ClickEvent event) {

		Object sender = event.getSource();

		if (sender == image_icon) {
			event.stopPropagation();
			presenter.centrerMapSurEtape(etapeModel.getLatitude(), etapeModel.getLongitude());
		}
		if (sender == image_update) {
			event.stopPropagation();
			formEditEtapeWidget.setEtapeModel(etapeModel);
			formEditEtapeWidget.init();
			dialogBox_editEtape.center();
			dialogBox_editEtape.show();
		}
		if (sender == image_delete) {
			event.stopPropagation();

			SC.ask("Etes-vous sur de vouloir supprimer l'etape ?", new BooleanCallback() {

				@Override
				public void execute(Boolean value) {
					if (value) {
						etapeWidget.removeFromParent();
						presenter.onDeleteEtape(etapeModel);
					}
				}
			});
		}
	}

	/**
	 * @return the etapeModel
	 */
	public EtapeModel getEtapeModel() {
		return etapeModel;
	}

	/**
	 * @param etapeModel
	 *            the etapeModel to set
	 */
	public void setEtapeModel(EtapeModel etapeModel) {
		this.etapeModel = etapeModel;
	}

	@Override
	public void handleUpdateEtapeEvent(UpdateEtapeEvent event) {
		EtapeModel etapeModelUpdate = event.getEtapeModel();

		if (etapeModelUpdate.getId().equals(etapeModel.getId())) {
			setEtapeModel(etapeModelUpdate);
			init();
		}
	}

	@Override
	public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
		return addDomHandler(handler, MouseOutEvent.getType());
	}

	@Override
	public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
		return addDomHandler(handler, MouseOverEvent.getType());
	}

	public void majPanelActions(boolean disabled) {
		image_detail.setStyleDependentName("disabled", disabled);
		image_update.setStyleDependentName("disabled", disabled);
		image_delete.setStyleDependentName("disabled", disabled);
	}

	@Override
	public void handleAddTrajetEvent(AddTrajetEvent event) {

		final TrajetModel trajetModel = event.getTrajetModel();
		
		if (event.getTrajetModel().getEtapeId().equals(etapeModel.getId())) {
			
			Button button_deleteTrajet = new Button("X");
			
			button_deleteTrajet.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					presenter.onDeleteTrajet(trajetModel);
				}
			});
			
			panel_actions.add(button_deleteTrajet);
		}
	}
}
